package org.sean.framework.canary.loadbalancer;

import org.sean.framework.canary.loadbalancer.predicate.AbstractServerPredicate;
import org.sean.framework.logging.Logger;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpHeaders;

public abstract class CanaryPredicate extends AbstractServerPredicate {
    protected Logger logger = Logger.newInstance(getClass());

    @Override
    public boolean apply(PredicateKey input) {
        if (input == null) {
            return false;
        }
        try {
            return getSelector(input.getServiceInstance(), input.getHeaders());
        } catch (Exception e) {
            logger.warn("match result : true -> Exception", e);
            return true;
        }
    }

    /**
     * 选择服务
     *
     * @param server  服务
     * @param headers header
     * @return 是否可选
     */
    public abstract boolean getSelector(ServiceInstance server, HttpHeaders headers);
}
